import { playerDtMgr } from '../../Framework/Data/DataBaseMgr';
import DataMgr from '../../Framework/Data/DataMgr';
import ResMgr from '../../Framework/Res/ResMgr';
import UIBase from '../../Framework/UIFramework/UIBase';
import Game from '../Game/Game';
const { ccclass, property } = cc._decorator;

@ccclass
export default class Menu extends UIBase {
    private _arrRoleNode = [];
    private _arrArrowNode: cc.Node[] = [];
    private _arrArrowIndex: number[] = [0, 3];
    private _arrlocked: Boolean[] = [false, false];

    private _arrKeyObj = [
        {
            65: "left",
            68: "right",
            32: "locked"
        },
        {
            37: "left",
            39: "right",
            13: "locked"
        }
    ]

    private _DirObj = {
        left: -1,
        right: 1,
    }

    onInit(...rest: any[]): void {
        this._initData();
        this._onKeyKeyboardEvent();
        this.onClickListner("_BtnStart", () => {
            //检查玩家是否都已锁定
            let result = this.checkRoleSelect();
            if (result) {
                let infor = "第" + (result) + "个玩家未锁定"
                this.openUI("RoleClockTip", false, false, "", "Window", infor);
                return;
            }
            //将玩家所选角色索引存储
            DataMgr.instance.getMgr<playerDtMgr>("playerDtMgr").arrPlayerIndex = this._arrArrowIndex;
            let index = this.getComp<cc.PageView>("_MapSelect", "PageView").getCurrentPageIndex();
            let levelDtMgr = DataMgr.instance.getMgr("levelDtMgr");
            levelDtMgr.curIndex = index;
            let gamePrefab = ResMgr.instance.getRes<cc.Prefab>("Game", "Game");
            let gameNode = cc.instantiate(gamePrefab);
            gameNode.parent = cc.find("Canvas");
            gameNode.setSiblingIndex(-100);
            let gameTs = gameNode.getComponent(Game);
            this.openUI("MainUI", true, true);
        });
    }


    checkRoleSelect() {
        for (let i = 0; i < this._arrlocked.length; i++) {
            if (this._arrlocked[i] == false) {
                return i + 1;
            }
        }
        return 0;
    }

    private _initData() {
        for (let i = 1; i <= 4; i++) {
            let node = this.getNode("_P" + i);
            this._arrRoleNode.push(node);
        }

        for (let i = 1; i <= 2; i++) {
            let node = this.getNode("_Arrow" + i);
            this._arrArrowNode.push(node);
        }
    }

    private _onKeyKeyboardEvent() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }


    private _removeKeyboardEvent() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }
    onKeyDown(event) {
        console.log(event.keyCode);
        for (let i = 0; i < this._arrKeyObj.length; i++) {
            let dir = this._arrKeyObj[i][event.keyCode];
            if (!dir) {
                continue;
            }
            if (this._arrlocked[i]) {
                //如果这个玩家被锁定了，就不能需要修改这个玩家的箭头索引
                let infor = "第" + (i + 1) + "个玩家已锁定,无法修改"
                this.openUI("RoleClockTip", false, false, "", "Window", infor);
                continue;
            }

            if (dir == "locked") {
                //如果玩家被锁定，很后面的逻辑就不执行了
                this._arrlocked[i] = true;
                let infor = "第" + (i + 1) + "个玩家已锁定"
                this.openUI("RoleClockTip", false, false, "", "Window", infor);
                return;
            }
            //设置箭头指向角色的索引
            let deltaArrow = this._DirObj[dir];
            this._arrArrowIndex[i] += deltaArrow;
            this._arrArrowIndex[i] = this._arrArrowIndex[i] < 0 ? this._arrRoleNode.length - 1 : this._arrArrowIndex[i];
            this._arrArrowIndex[i] = this._arrArrowIndex[i] > this._arrRoleNode.length - 1 ? 0 : this._arrArrowIndex[i];
            break;
        }
        //设置箭头的位置
        for (let i = 0; i < this._arrArrowIndex.length; i++) {
            this._arrArrowNode[i].x = this._arrRoleNode[this._arrArrowIndex[i]].x;
            if (this._arrArrowNode[0].x == this._arrArrowNode[1].x) {
                this._arrArrowNode[0].x -= 15;
                this._arrArrowNode[1].x += 15;
                break;
            }
        }

    }
    onKeyUp() {

    }
    onExit(...rest: any[]): void {
        this._removeKeyboardEvent();
    }
}


